/**
 * Copyright &copy; 2012-2016 civet All rights reserved.
 */
package cn.mcsys.myadmin.modules.goods.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.mcsys.myadmin.common.persistence.Page;
import cn.mcsys.myadmin.modules.goods.entity.GoodsCategory;
import cn.mcsys.myadmin.modules.goods.service.GoodsCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.mcsys.myadmin.common.config.Global;
import cn.mcsys.myadmin.common.persistence.Page;
import cn.mcsys.myadmin.common.web.BaseController;
import cn.mcsys.myadmin.common.utils.StringUtils;
import cn.mcsys.myadmin.modules.goods.entity.GoodsObj;
import cn.mcsys.myadmin.modules.goods.service.GoodsObjService;

import java.util.List;

/**
 * 商品信息Controller
 * @author Jockey
 * @version 2016-11-20
 */
@Controller
@RequestMapping(value = "${adminPath}/goods/goodsObj")
public class GoodsObjController extends BaseController {

	@Autowired
	private GoodsObjService goodsObjService;
	@Autowired
	private GoodsCategoryService goodsCategoryService;
	
	@RequiresPermissions("goods:goodsObj:view")
	@RequestMapping(value = {"list", ""})
	public String list(GoodsObj goodsObj, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<GoodsObj> page = goodsObjService.findPage(new Page<GoodsObj>(request, response), goodsObj);
		model.addAttribute("page", page);
		return "modules/goods/goodsObjList";
	}

	@RequiresPermissions("goods:goodsObj:view")
	@RequestMapping(value = "form")
	public String form(GoodsObj goodsObj, Model model) {
		GoodsCategory gc = new GoodsCategory();

	    if (goodsObj.getId() != null){
            goodsObj = goodsObjService.get(goodsObj.getId());
			//分类回显
			if(goodsObj.getCategoryId() != null){
				gc.setParent(goodsCategoryService.get(goodsObj.getCategoryId()));
				if(gc.getId() == null){
					int size = 0;
					List<GoodsCategory> list = goodsCategoryService.findAllGoodsCategory();
					for (int i=0; i<list.size(); i++){
						GoodsCategory g = list.get(i);
						if(g.getParent() != null && g.getParent().getId() != null && g.getParent().getId().equals(gc.getParent().getId())){
							size ++;
						}
					}
				}
			}
        }
        model.addAttribute("goodsCategory",gc);
		model.addAttribute("goodsObj", goodsObj);
		return "modules/goods/goodsObjForm";
	}

	@RequiresPermissions("goods:goodsObj:edit")
	@RequestMapping(value = "save")
	public String save(GoodsObj goodsObj, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, goodsObj)){
			return form(goodsObj, model);
		}
		if(goodsObj.getParent().getId() != null && !"".equals(goodsObj.getParent().getId())){
			goodsObj.setCategoryId(goodsObj.getParent().getId());
		}
		goodsObjService.save(goodsObj);
		addMessage(redirectAttributes, "保存商品信息成功");
		return "redirect:"+Global.getAdminPath()+"/goods/goodsObj/?repage";
	}
	
	@RequiresPermissions("goods:goodsObj:edit")
	@RequestMapping(value = "delete")
	public String delete(GoodsObj goodsObj, RedirectAttributes redirectAttributes) {
		goodsObjService.delete(goodsObj);
		addMessage(redirectAttributes, "删除商品信息成功");
		return "redirect:"+Global.getAdminPath()+"/goods/goodsObj/?repage";
	}

}